import sensor, image, time, math
from pyb import UART
#thresholds是指颜色阈值，把你想要检测的颜色阈值写成一个元组放进这个list
thresholds = [
              #(0, 25, 28, 49, 41, 90),
              #(14, 87, -83, 127, -113, -14),
              #(30, 44, -46, -9, 7, 44),
              (0, 100, -127, 0, 0, 127)] #三个阈值更准确些
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
while(True):
    clock.tick()
    img = sensor.snapshot()
    uart = UART(3,115200)
    uart.init(115200,bits=8,parity=None,stop=1)
    deflection_math= 0
    A=deflection_math
    for blob in img.find_blobs(thresholds,pixels_threshold=200,area_threshold=200):
        if blob.density()>0.80:
            A=0
        elif blob.density()>0.60:
            A= 0
        elif blob.density()>0.40:
            A= 3
            print("检测为三角型  ",end='')
            img.draw_cross(blob.cx(), blob.cy())
            print('三角型边长',blob.w())
        else:
            A= 0
            #print("no dectedtion")
        if blob.w()<78:
            A= 0
        if blob.w()>100:
            A= 0
    uart_buf = bytearray([int (A)])
    uart.write(uart_buf)
    time.sleep(0)#延时
